package fragment.menufragments;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.LinearLayout;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.lanou.horo.myapplication.R;

import java.util.List;

import entity.MunicipaUnitsSingotenEntity;
import fragment.AbsBaseFragment;
import utils.viewtools.SpacesItemDecoration;
import utils.adapter.MunicipaUnitsSingotenAdapter;
import volley.VolleySingleton;

/**
 * Created by dllo on 15/12/3.
 */
public class MunicipalUnitsSingotenFragment extends AbsBaseFragment {
    //    private static MunicipalUnitsSingotenFragment fragment;
    private long code;
    private RecyclerView recyclerView;
    private MunicipaUnitsSingotenAdapter adapter;

    public MunicipalUnitsSingotenFragment() {
    }

    public static MunicipalUnitsSingotenFragment getInstance(long code) {
        MunicipalUnitsSingotenFragment f = new MunicipalUnitsSingotenFragment();
        Bundle data = new Bundle();
        data.putLong("code", code);
        f.setArguments(data);
        return f;
    }

//    public static MunicipalUnitsSingotenFragment getFragment(int position) {
//        if (fragment == null){
//            synchronized (MunicipalUnitsTitleEntity.class){
//                if (fragment == null){
//                    return new MunicipalUnitsSingotenFragment(position);
//                }
//            }
//        }
//        return fragment;
//    }

    @Override
    protected int getLayoutView() {
        return R.layout.fragment_units_singoten;
    }

    @Override
    protected void initView() {
        recyclerView = findView(R.id.unitsSingotenRecyclerview);

    }

    @Override
    protected void initData() {
        code = getArguments().getLong("code");
        adapter = new MunicipaUnitsSingotenAdapter(getContext());
        GridLayoutManager manager = new GridLayoutManager(getContext(), 1);
        manager.setOrientation(LinearLayout.VERTICAL);
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(adapter);
        SpacesItemDecoration decoration = new SpacesItemDecoration(00);
        recyclerView.addItemDecoration(decoration);
        initNetwork();
    }

    private void initNetwork() {
        String URL = "http://live.ximalaya.com/live-web/v1/getRadiosListByType?" +
                "pageNum=1&radioType=2&device=android&provinceCode="+code+"&pageSize=15";
        VolleySingleton.getInstance().addRequest(URL, MunicipaUnitsSingotenEntity.class,
                new Response.Listener<MunicipaUnitsSingotenEntity>() {
                    @Override
                    public void onResponse(MunicipaUnitsSingotenEntity response) {
                        if (response != null) {
                            List<MunicipaUnitsSingotenEntity.ResultEntity> list = response.getResult();
                            if (list != null) {
                                adapter.setList(list);
                            }
                        }
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });
    }
}
